package com.tcs.process;

import com.tcs.annotation.MapOfKey;
import com.tcs.annotation.NettyContoller;
import com.tcs.service.action.Action;
import com.tcs.util.staticpack.StaticPack;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.reflect.Method;

/**
 * 把所有的Bean进行初始化.做一些事情
 */
@Component(value="dataStanProcess")
public class DataStanProcess implements BeanPostProcessor {

    @Resource(name="staticPack")
    private StaticPack<Integer,Action> staticPack;

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        NettyContoller nettyContoller = bean.getClass().getAnnotation(NettyContoller.class);
        if (null != nettyContoller) {
            Method[] methods = bean.getClass().getMethods();
            for (Method method : methods) {
                MapOfKey mapOfKey = method.getAnnotation(MapOfKey.class);
                if (null != mapOfKey) {
                    Action action = new Action();
                    action.setBean(bean);
                    action.setMethod(method);
                    staticPack.put(mapOfKey.key(),action);
                }
            }
        }
        return bean;
    }
}
